<?php
session_start();
require('settings.php');
require('dbConn.php');
$user_session = explode("_", $_SESSION['user']);
$user_id = $user_session[0];

if(isset($_SESSION['user'])){

	$sql = "SELECT mobile, mobile_verified, verification FROM users WHERE id = '".$user_id."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$rs=mysql_fetch_assoc($query);

if($rs['mobile_verified'] == '0' && strlen($rs['mobile']) > 0){

// load in settings

$settingsQuery = "SELECT * FROM settings";
$settingsResult=mysql_query( $settingsQuery ) or die ("Error in query: $settingsQuery. " . mysql_error());
while($settingsArr=mysql_fetch_assoc($settingsResult)){
$settings[$settingsArr['setting_name']] = $settingsArr['setting_value'];
}

if(strlen($rs['verification']) != 6){

	$md5_hash = md5(rand(0,999));
	$txt_code = strtoupper(substr($md5_hash, 15, 6));
	$txt_code_remove = array('0','O');
	$txt_code = str_replace($txt_code_remove, 'Y', $txt_code);

	$sql = "UPDATE users SET verification = '".$txt_code."' WHERE id = '".$user_id."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

	require_once("../classes/class.phpmailer.php");
	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = $settings['sms_email'];
	$mail->FromName = $settings['sms_email'];

	$mail->AddAddress("44".substr($rs['mobile'], 1)."@bulksms.co.uk");

	$mail->Subject = $settings['sms_password'];
	$mail->Body = "To verify your mobile on I Want A Tradesman, please enter the following code: ".$txt_code;

	$mail->Send();

/* HTTP POST METHOD

	$msisdn = "44".substr($rs['mobile'], 1);
	$txt_message = "To verify your mobile on I Want A Tradesman, please enter the following code: ".$txt_code;

	$url = 'http://www.bulksms.co.uk/eapi/submission/send_sms/2/2.0';
	$port = 80;

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_PORT, $port);
	curl_setopt ($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

	$post_body = '';

	$post_fields = array(
		username => $settings['sms_username'],
		password => $settings['sms_password'],
		message => $txt_message,
		msisdn => $msisdn,
		routing_group => '3'
	);

	foreach($post_fields as $key=>$value) {
		$post_body .= urlencode($key).'='.urlencode($value).'&';
	}
	$post_body = rtrim($post_body,'&');

	curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_body);
	$response_string = curl_exec($ch);
	$curl_info = curl_getinfo($ch);
	curl_close($ch);
*/
}

else{

$already_sent = ' already';

}

	echo '<h1>Verify mobile phone</h1>
	<div class="popup_close"><a onclick="return popupDisplayNone(\'popupDiv\');"><img src="/images/cross.png" alt="Close" title="Close" /></a></div>
	<p style="padding: 10px 15px 15px 15px; font-size: 11px;">We have'.$already_sent.' sent you a text message containing your verification code, please enter the code below to verify your mobile.<br /><br /><strong>Note:</strong> Message delivery can take from 1 - 30 minutes. You can return here at anytime to enter your code.</p>

	<p style="display: none; color: #cc0000; font-weight: bold; font-size: 11px; padding-bottom: 10px; padding-left: 15px;" id="verify_error"></p>

	<div class="formRowWrapper">
	<div class="formRowLeft"><label for="txt_code">Code:</label></div>
	<div class="formRowRight"><input type="text" id="txt_code" class="textbox" style="width: 60px;" /></div>
	</div>

	<div class="formRowWrapper" style="text-align: right;">
	<a onclick="return verifyMobile2();" style="margin-right: 25px; cursor: pointer;"><img src="/images/verify_button.gif" alt="Verify" /></a>
	</div>
	';
}

}

?>